package com.ruoyi.project.api.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.goods.goods.domain.Goods;
import com.ruoyi.project.goods.goods.service.IGoodsService;
import com.ruoyi.project.system.config.domain.Config;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/api/goods")
public class IGoodsController extends BaseController {

    @Autowired
    private IGoodsService goodsService;

    @ResponseBody
    @RequestMapping("list")
    public TableDataInfo list(Config config)
    {
        System.out.println("list");
        //startPage();
        //List<Config> list = configService.selectConfigList(config);
        //return getDataTable(list);
        return null;
    }

    @ResponseBody
    @PostMapping("addGoods")
    public AjaxResult addGoods(@RequestParam("file") MultipartFile file,Goods goods) throws IOException
    {
        System.out.println("新增Goods");
        if(goods!=null){
            if(StringUtils.converNull(goods.getBarCode()).equals("")){
                return AjaxResult.error("条形码不能为空");
            }
        }
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        goods.setImgPath(fileName);
        int result=goodsService.insertGoods(goods);
        return  toAjax(result);
    }

    /**
     * 查询商品
     * @param styleNumber
     * @return
     * @throws IOException
     */
    @ResponseBody
    @PostMapping("selectGoodsByStyleNumber")
    public AjaxResult selectGoodsByStyleNumber(String styleNumber) throws IOException
    {
        AjaxResult result=new AjaxResult();
        List<Goods> list=new ArrayList<Goods>();
        String msg="";
        //必须输入4位，再like
        if(styleNumber.length()>=4){
            Goods goods=new Goods();
            goods.setStyleNumber(styleNumber);
            list= goodsService.selectGoodsList(goods);
        }
        result.put("msg",msg);
        result.put("list",list);
        return result;
    }

}
